package pers.vic.quartz.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vic.boot.base.service.BaseService;
import pers.vic.boot.base.vo.BooleanWithMsg;
import pers.vic.quartz.constant.QuartzStatusEnum;
import pers.vic.quartz.mapper.QuartzTaskJobMapper;
import pers.vic.quartz.model.QuartzTaskJob;

/**
 * @description:定时任务 Service
 * @author Vic.xu
 * @date: 2022-04-13
 */
@Service
public class QuartzTaskJobService extends BaseService<QuartzTaskJobMapper, QuartzTaskJob> {

    @Autowired
    private QuartzManagerService quartzManagerService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }


    /**
     * 新增的时候 加如定时任务 并执行
     */
    @Override
    public int insert(QuartzTaskJob entity) {
        int num = super.insert(entity);
        quartzManagerService.addJob(entity);
        return num;
    }

    /**
     *  变换定时任务定义的时候 做响应的处理
     */
    @Override
    public int update(QuartzTaskJob entity) {
        QuartzTaskJob old = findById(entity.getId());
        int num = super.update(entity);
        //默认不改变key和group  故此处不判断
        boolean someClazz = StringUtils.equals(old.getClazz(), entity.getClazz());
        boolean someCron = StringUtils.equals(old.getCron(), entity.getCron());
        //如果执行的类都变了 则直接删除原来的 新增新的定时任务
        if (!someClazz) {
            quartzManagerService.removeJob(old);
            quartzManagerService.addJob(entity);

        } else if (!someCron) {
            //如果是cron表达式变化了
            quartzManagerService.modifyJobTime(entity);

        }
        return num;
    }

    @Override
    public int delete(int id) {
        quartzManagerService.removeJob(findById(id));
        return super.delete(id);
    }

    @Override
    public int delete(int[] ids) {
        findByIds(ids).forEach(quartzManagerService::removeJob);
        return super.delete(ids);
    }

    /**
     * 操作定时任务
     * @param id 任务表的id
     * @param status 需要进行的操作 @see QuartzStatusEnum
     * @return 是否操作成功
     */
    public BooleanWithMsg operate(int id, int status) {
        QuartzTaskJob info = findById(id);
        if (info == null) {
            return BooleanWithMsg.fail("不存在的任务");
        }
        QuartzStatusEnum statusEnum = QuartzStatusEnum.getByStatus(status);
        if (statusEnum == null) {
            return BooleanWithMsg.fail("不合法的任务状态");
        }
        BooleanWithMsg result = beforeOperate(info, statusEnum);
        if (!result.isSuccess()) {
            return result;
        }
        switch (statusEnum) {
            case pause:
                result = quartzManagerService.pauseJob(info);
                break;
            case working:

                result = quartzManagerService.resumeJob(info);
                break;
            case remove:
                result = quartzManagerService.removeJob(info);
                break;
            default:
        }
        //修改表 状态字段
        if (result.isSuccess()) {
            info.setStatus(status);
            update(info);
        }
        return result;
    }

    public BooleanWithMsg beforeOperate(QuartzTaskJob info, QuartzStatusEnum statusEnum) {
        if (info.getStatus() == statusEnum.getStatus()) {
            return BooleanWithMsg.fail("当前任务状态已经是" + statusEnum.getText());
        }
        boolean exist = quartzManagerService.exist(info);
        //对于不存在的任务 应该先添加到任务中
        if (!exist) {
            quartzManagerService.addJob(info);
        }

        return BooleanWithMsg.success();

    }

    /**
     * 立刻执行任务
     * @param id
     * @return boolean
     */
    public BooleanWithMsg runJob(int id) {
        QuartzTaskJob info = findById(id);
        if (info == null) {
            return BooleanWithMsg.fail("不存在的任务");
        }
        boolean exist = quartzManagerService.exist(info);
        //对于不存在的任务 应该先添加到任务中
        if (!exist) {
            quartzManagerService.addJob(info);
        }
        BooleanWithMsg msg = quartzManagerService.runJobNow(info);
        return msg;
    }
}
